home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
pctj8411.arc
/
STACK.MOD
< prev
next >
Wrap
Text File
|
1986-09-14
|
1KB
|
45 lines
MODULE StackIllustration;
(*
This program module illustrates how the services of the library
module StackHandler can be used to implement a very simple stack-based
adding machine. Note how we can declare a variable, S, of type
Stack without knowing how stacks are actually implemented (arrays,
linked lists, etc.). We only care to know that the procedures Push,
Pop, etc. work as claimed. This module also imports I/O services
from the standard library module InOut.
*)
FROM InOut IMPORT Read, WriteString, WriteLn, ReadInt, WriteInt;
FROM StackHandler IMPORT Stack, Initialize, Push, Pop, Empty;
VAR
S : Stack;
i : INTEGER;
ch : CHAR;
BEGIN
(* Initialize stack for further work. *)
Initialize(S);
(* Collect input data, push onto stack. *)
REPEAT
WriteString("Enter: ");
ReadInt(i);
WriteLn;
Push(i, S);
WriteString("Done?");
Read(ch);
WriteLn;
UNTIL (ch = "Y");
(* Initialize i then accumulate sum of stack elements. *)
i := 0;
WHILE NOT Empty(S) DO
i := i + Pop(S);
END;
(* Write sum in a field 7 digits wide. *)
WriteString("Sum= ");
WriteInt(i, 7);
WriteLn;
END StackIllustration.